/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.adapter.zhuanquanquan;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.xuexiang.xui.widget.button.roundbutton.RoundDrawable;
import com.xuexiang.xui.widget.layout.XUILinearLayout;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.entity.Decision;
import com.xuexiang.xuidemo.entity.ResultAndWeight;
import com.xuexiang.xuidemo.fragment.zhuanquanquan.MyDecisionFragment;
import com.xuexiang.xuidemo.fragment.zhuanquanquan.SetDecisionFragment;
import com.xuexiang.xuidemo.utils.ZhuanpanSPUtils;

import java.util.List;

public class ZhuanpanAdapter extends ArrayAdapter<Decision> {

    private List<Decision> decisions;
    private int resId;
    private MyDecisionFragment myDecisionFragment;
    private ZhuanpanSPUtils zInstance = ZhuanpanSPUtils.getInstance();

    public ZhuanpanAdapter(@NonNull Context context, int resource, @NonNull List<Decision> objects,MyDecisionFragment fragment) {
        super(context, resource, objects);
        decisions = objects;
        resId = resource;
        myDecisionFragment = fragment;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Decision item = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resId, parent, false);

        XUILinearLayout layout = (XUILinearLayout)view.findViewById(R.id.layout_item_decision);
        layout.setRadiusAndShadow(DensityUtils.dp2px(getContext(), 8),20,0.80f);

        TextView title = (TextView)view.findViewById(R.id.text_item_decision);
        title.setText(decisions.get(position).getTitle());
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zInstance.setChoice(decisions.get(position).getId());
                myDecisionFragment.popToBack();
            }
        });

        RoundButton update = (RoundButton)view.findViewById(R.id.update_item_decision);
        update.setOnClickListener(v -> myDecisionFragment.openPage(SetDecisionFragment.class,"key",decisions.get(position).getId()));
        //RoundDrawable background = (RoundDrawable)update.getBackground();
        //background.setStroke(10, Color.parseColor("#96db96"));

        RoundButton delete = (RoundButton)view.findViewById(R.id.delete_item_decision);
        delete.setOnClickListener(v -> {
            zInstance.deleteDecision(decisions.get(position).getId());
            decisions.remove(position);
            ZhuanpanAdapter.this.notifyDataSetChanged();
        });
        return view;
    }

    interface BindFragment {
        MyDecisionFragment getFragment();
    }
}
